const express=require('express')
const router= express.Router();
const db=require('../../utils/db')

module.exports = router

router.get('/getworker',function(req,res,next){
  let mysql=`SELECT *FROM USER`;
  db(mysql,  function(err, data) {
    console.log('err', err);
    if (err) {
        return res.json({
            code: 500,
            msg: '查询失败'
        })
    }
    res.json({
      code: 200,
      msg: '查询成功',
      data: data
  })
})
})

//删除

// router.post('/remove',function(req,res,next){
// let mysql=`delete from user where user_id='${req.body.id}'`
// db(mysql,function(err,data){
//   if(err){
//     return res.json({
//       code: 500,
//       msg: '删除数据失败'
//   })
//   }
//   res.json({
//     code: 200,
//     msg: '删除数据成功',
//     data: data
// })
// })
// })


router.post('/remove',function(req,res,next){
  
  let mysql1=`UPDATE stock_check SET check_maker = NULL WHERE check_maker = ${req.body.id}`
  db(mysql1,function(err,data){
    if(err){
      return res.json({
        code: 500,
        msg: '删除数据失败11'
    })
    }
  let mysql2=`UPDATE order_information SET order_user_id = NULL WHERE order_user_id = ${req.body.id}`
  db(mysql2,function(err,data){
    if(err){
      return res.json({
        code: 500,
        msg: '删除数据失败22'
    })
    }
    let mysql3=`UPDATE line_up SET line_name= NULL WHERE line_name = ${req.body.id}`
    db(mysql3,function(err,data){
      if(err){
        return res.json({
          code: 500,
          msg: '删除数据失败1'
      })
      }
      let mysql4=`UPDATE package_list SET pack_founder= NULL WHERE pack_founder = ${req.body.id}`
      db(mysql4,function(err,data){
        if(err){
          return res.json({
            code: 500,
            msg: '删除数据失败2'
        })
        }
        let mysql5=`UPDATE discounts_management SET dis_operator= NULL WHERE dis_operator= ${req.body.id}`
        db(mysql5,function(err,data){
          if(err){
            return res.json({
              code: 500,
              msg: '删除数据失败55'
          })
          }
          let mysql5=`UPDATE dish_flowing SET dish_cashier= NULL WHERE dish_cashier= ${req.body.id}`
          db(mysql5,function(err,data){
            if(err){
              return res.json({
                code: 500,
                msg: '删除数据失败355'
            })
            }
            let mysql5=`delete  from  user where user_id='${req.body.id}'`
            db(mysql5,function(err,data){
              if(err){
                return res.json({
                  code: 500,
                  msg: '删除数据失败3'
              })
              }
              res.json({
                code: 200,
                msg: '删除数据成功',
                data1:data
            })
          
          })
    
        })
      })
    })
  })
})
  })
})


//修改
router.post('/edit',function(req,res,next){
  let mysql=`UPDATE  user SET user_tel ='${req.body.tel}',user_password = '${req.body.password}',user_company='${req.body.shop}' WHERE user_id ='${req.body.id}' `
  db(mysql,function(err,data){
    if(err){
      return res.json({
        code: 500,
        msg: '修改数据失败'
      })
    }
    res.json({
      code: 200,
      msg: '修改数据成功',
      data: data
  })
  })
})

//新增
router.post('/getuser',function(req,res,next){
  let mysql=`INSERT INTO USER (user_num,user_name,user_tel,user_company,user_work_time,user_password) VALUES('${req.body.num}','${req.body.name}','${req.body.tel}','${req.body.shop}','${req.body.time}','${req.body.password}')`
  db(mysql,function(err,data){
    if(err){
      return res.json({
        code: 500,
        msg: '添加数据失败'
      })
    }
    res.json({
      code: 200,
      msg: '添加数据成功',
      
  })
  })

})